package typeConversion

import (
	"errors"
	"fmt"
	"strconv"
)

func ToInt32(p_in interface{}) (int32, error) {
	if p_in == nil {
		return 0, errors.New("too.typeConversion ToInt err: Parameters not supported, Parameters is nil")
	}
	switch p_in.(type) {
	case string:
		int, err := strconv.ParseInt(p_in.(string), 10, 32)
		if err != nil {
			return 0, err
		} else {
			return int32(int), nil
		}
	case int:
		return int32(p_in.(int)), nil
	case int32:
		return p_in.(int32), nil
	case int64:
		return int32(p_in.(int32)), nil
	case float32:
		return int32(p_in.(float32)), nil
	case float64:
		return int32(p_in.(float64)), nil
	default:
		return 0, errors.New("too.typeConversion ToInt32 err: Parameters not supported,The parameter type is " + fmt.Sprintf("%T", p_in))
	}
}
